/**
 *
 */
import Util;
import OpenApi;
import OpenApiUtil;
import EndpointUtil;

extends OpenApi;


init(config: OpenApi.Config){
  super(config);
  @endpointRule = '';
  
  checkConfig(config);
  @endpoint = getEndpoint('imagesearch', @regionId, @endpointRule, @network, @suffix, @endpointMap, @endpoint);
}

function getEndpoint(productId: string, regionId: string, endpointRule: string, network: string, suffix: string, endpointMap: map[string]string, endpoint: string) throws: string{
  if (!Util.empty(endpoint)) {
    return endpoint;
  }
  
  if (!Util.isUnset(endpointMap) && !Util.empty(endpointMap[regionId])) {
    return endpointMap[regionId];
  }
  return EndpointUtil.getEndpointRules(productId, regionId, endpointRule, network, suffix);
}

model AddImageRequest {
  categoryId?: int32(name='CategoryId', example='88888888'),
  crop?: boolean(name='Crop', example='true'),
  customContent?: string(name='CustomContent', example='{\\"merchant_id\\":\\"46-718f-da04-4099b2f144ae\\",\\"category_ids\\":[\\"1234\\"],\\"image_index\\":0,\\"updated\\":5678}'),
  instanceName?: string(name='InstanceName', example='demoinstance1'),
  intAttr?: int32(name='IntAttr', example='22'),
  picContent?: string(name='PicContent', example='AAAANSUhEUgAAAPcAAAEVCAYAAAA8d3NuAAAAAXNSR0IArs......RK5CYII='),
  picName?: string(name='PicName', example='2092061_1.jpg'),
  productId?: string(name='ProductId', example='2092061_1'),
  region?: string(name='Region', example='280,486,232,351'),
  strAttr?: string(name='StrAttr', example='ss'),
}

model AddImageResponseBody = {
  code?: int32(name='Code', example='0'),
  message?: string(name='Message', example='success'),
  picInfo?: {
    categoryId?: int32(name='CategoryId', example='88888888'),
    region?: string(name='Region', example='94,691,206,650'),
  }(name='PicInfo'),
  requestId?: string(name='RequestId', example='E0845DE6-52AF-4B50-9F15-51ED4044E6AB'),
  success?: boolean(name='Success', example='true'),
}

model AddImageResponse = {
  headers?: map[string]string(name='headers'),
  statusCode?: int32(name='statusCode'),
  body?: AddImageResponseBody(name='body'),
}

async function addImageWithOptions(request: AddImageRequest, headers: map[string]string, runtime: Util.RuntimeOptions): AddImageResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.categoryId)) {
    body['CategoryId'] = request.categoryId;
  }
  if (!Util.isUnset(request.crop)) {
    body['Crop'] = request.crop;
  }
  if (!Util.isUnset(request.customContent)) {
    body['CustomContent'] = request.customContent;
  }
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.intAttr)) {
    body['IntAttr'] = request.intAttr;
  }
  if (!Util.isUnset(request.picContent)) {
    body['PicContent'] = request.picContent;
  }
  if (!Util.isUnset(request.picName)) {
    body['PicName'] = request.picName;
  }
  if (!Util.isUnset(request.productId)) {
    body['ProductId'] = request.productId;
  }
  if (!Util.isUnset(request.region)) {
    body['Region'] = request.region;
  }
  if (!Util.isUnset(request.strAttr)) {
    body['StrAttr'] = request.strAttr;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'AddImage',
    version = '2019-03-25',
    protocol = 'HTTPS',
    pathname = `/v2/image/add`,
    method = 'POST',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function addImage(request: AddImageRequest): AddImageResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return addImageWithOptions(request, headers, runtime);
}

model DeleteImageRequest {
  instanceName?: string(name='InstanceName', example='demoinstance1'),
  picName?: string(name='PicName', example='2092061_1.jpg'),
  productId?: string(name='ProductId', example='179443'),
}

model DeleteImageResponseBody = {
  code?: int32(name='Code', example='0'),
  message?: string(name='Message', example='success'),
  requestId?: string(name='RequestId', example='0703956F-9BCC-48FA-99F7-96C0BF449C69'),
  success?: boolean(name='Success', example='true'),
}

model DeleteImageResponse = {
  headers?: map[string]string(name='headers'),
  statusCode?: int32(name='statusCode'),
  body?: DeleteImageResponseBody(name='body'),
}

async function deleteImageWithOptions(request: DeleteImageRequest, headers: map[string]string, runtime: Util.RuntimeOptions): DeleteImageResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.picName)) {
    body['PicName'] = request.picName;
  }
  if (!Util.isUnset(request.productId)) {
    body['ProductId'] = request.productId;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'DeleteImage',
    version = '2019-03-25',
    protocol = 'HTTPS',
    pathname = `/v2/image/delete`,
    method = 'POST',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function deleteImage(request: DeleteImageRequest): DeleteImageResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return deleteImageWithOptions(request, headers, runtime);
}

model SearchImageRequest {
  categoryId?: int32(name='CategoryId', example='88888888'),
  crop?: boolean(name='Crop', example='true'),
  filter?: string(name='Filter', example='(int_attr >= 100000000 AND int_attr < 200000000 AND int_attr=100000252 AND str_attr=\\"87\\")'),
  instanceName?: string(name='InstanceName', example='phimagesearch'),
  num?: int32(name='Num', example='10'),
  picContent?: string(name='PicContent', example='iVBORw0KGgoAAAANSUhEUgAAAV8AAADwAQMAAACdRvVNAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAADUExURQAAAKd6PdoAAAABdFJOUyR85Ty3AAAAIUlEQVRo3u3BAQ0AAADCoPdPbQ8HFAAAAAAAAAAAAADPBiowAAGR02EbAAAAAElFTkSuQmCC'),
  picName?: string(name='PicName', example='goods-23312-1'),
  productId?: string(name='ProductId', example='pro-goods-25376'),
  region?: string(name='Region', example='32,282,43,256'),
  start?: int32(name='Start', example='0'),
  type?: string(name='Type', example='SearchByPic'),
}

model SearchImageResponseBody = {
  auctions?: [ 
    {
      categoryId?: int32(name='CategoryId', example='8888888'),
      customContent?: string(name='CustomContent', example='zidingyi'),
      intAttr?: int32(name='IntAttr', example='2'),
      picName?: string(name='PicName', example='2092061_1.jpg'),
      productId?: string(name='ProductId', example='2092061_1'),
      score?: float(name='Score'),
      sortExprValues?: string(name='SortExprValues', example='5.37633353624177e+24;0'),
      strAttr?: string(name='StrAttr', example='ss'),
    }
  ](name='Auctions'),
  code?: int32(name='Code', example='0'),
  head?: {
    docsFound?: int32(name='DocsFound', example='10'),
    docsReturn?: int32(name='DocsReturn', example='10000'),
    searchTime?: int32(name='SearchTime', example='95'),
  }(name='Head'),
  msg?: string(name='Msg', example='success'),
  picInfo?: {
    allCategories?: [ 
      {
        id?: int32(name='Id', example='88888888'),
        name?: string(name='Name', example='other'),
      }
    ](name='AllCategories'),
    categoryId?: int32(name='CategoryId', example='88888888'),
    multiRegion?: [ 
      {
        region?: string(name='Region'),
      }
    ](name='MultiRegion'),
    region?: string(name='Region', example='94,691,206,650'),
  }(name='PicInfo'),
  requestId?: string(name='RequestId', example='3033A0A7-36DA-5AC9-9973-953C38EBDFA9'),
  success?: boolean(name='Success', example='true'),
}

model SearchImageResponse = {
  headers?: map[string]string(name='headers'),
  statusCode?: int32(name='statusCode'),
  body?: SearchImageResponseBody(name='body'),
}

async function searchImageWithOptions(request: SearchImageRequest, headers: map[string]string, runtime: Util.RuntimeOptions): SearchImageResponse {
  Util.validateModel(request);
  var body : map[string]any = {};
  if (!Util.isUnset(request.categoryId)) {
    body['CategoryId'] = request.categoryId;
  }
  if (!Util.isUnset(request.crop)) {
    body['Crop'] = request.crop;
  }
  if (!Util.isUnset(request.filter)) {
    body['Filter'] = request.filter;
  }
  if (!Util.isUnset(request.instanceName)) {
    body['InstanceName'] = request.instanceName;
  }
  if (!Util.isUnset(request.num)) {
    body['Num'] = request.num;
  }
  if (!Util.isUnset(request.picContent)) {
    body['PicContent'] = request.picContent;
  }
  if (!Util.isUnset(request.picName)) {
    body['PicName'] = request.picName;
  }
  if (!Util.isUnset(request.productId)) {
    body['ProductId'] = request.productId;
  }
  if (!Util.isUnset(request.region)) {
    body['Region'] = request.region;
  }
  if (!Util.isUnset(request.start)) {
    body['Start'] = request.start;
  }
  if (!Util.isUnset(request.type)) {
    body['Type'] = request.type;
  }

  var req = new OpenApi.OpenApiRequest{ 
    headers = headers,
    body = OpenApiUtil.parseToMap(body),
  };
  var params = new OpenApi.Params{
    action = 'SearchImage',
    version = '2019-03-25',
    protocol = 'HTTPS',
    pathname = `/v2/image/search`,
    method = 'POST',
    authType = 'AK',
    style = 'ROA',
    reqBodyType = 'formData',
    bodyType = 'json',
  };
  return callApi(params, req, runtime);
}

async function searchImage(request: SearchImageRequest): SearchImageResponse {
  var runtime = new Util.RuntimeOptions{};
  var headers : map[string]string = {};
  return searchImageWithOptions(request, headers, runtime);
}

